package com.ac.input.web.action.input;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionMapping;

//import com.ac.core.dm.User;
import com.caac.core.web.action.ViewForward;
import com.caac.core.web.form.BaseForm;
import com.ac.input.dao.BaseDao;
import com.ac.input.dao.ReportsDao;
import com.ac.input.web.action.BaseExportAction;
import com.ac.input.web.form.DataSearchForm;

/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright AgileCentury 2004</p>
 * <p>Company: AgileCentury</p>
 * @author <a href="mailto:lianzs@tom.com">Lianzs</a>
 * @version 1.0
 */

public class ExportDataAction extends BaseExportAction {
    protected ViewForward doViewExecute(ActionMapping mapping, BaseForm form,
                                        HttpServletRequest request,
                                        HttpServletResponse response) throws
            Exception {
        DataSearchForm myForm = (DataSearchForm) form;
/*        User user = null;//this.getUserFromCache(request, response);
        String orgId = (user == null) ? "" : user.getUnitId();
*/
        if (myForm.isFirst()) {
            ReportsDao rpsDao = (ReportsDao)this.getBean("reportsDao");
            myForm.setRhid(rpsDao.getHndInpDefById(myForm.getMendId()));
            myForm.setFirst(false);
            myForm.setAddWhere(null);
            if(!"".equals(myForm.getWhereId())){
            	String addWhere =(String) this.getJdbcTemplate().queryForObject("SELECT WR_CONT FROM HND_ADD_WHERE WHERE ID='"+myForm.getWhereId()+"'", String.class);
            	myForm.setAddWhere(addWhere);
            }
        }

        String sql = myForm.getRhid().getSchSql();
        String whereStr = " where 1=1 ";
/*        if (!orgId.equals("000000") && myForm.getOrgIdName() != null) {
            whereStr = " where " + myForm.getOrgIdName() + " IN "
                       + myForm.getOrgWhere(orgId);
        }
*/        
        if (myForm.getWhereStr()!=null && !"".equals(myForm.getWhereStr())) {
            whereStr += " and (" + myForm.getWhereStr() + ")";
        }

        if (myForm.getAddWhere()!=null && !"".equals(myForm.getAddWhere())) {
            whereStr += " and (" + myForm.getAddWhere() + ")";
        }

        String orderStr = myForm.getOrderStr();
        if (!"".equals(myForm.getOrderStr())) {
            orderStr = " order by " + myForm.getOrderStr();
        }
        String viewStr =" * ";

        BaseDao baseDao = (BaseDao)this.getBean("hndBaseDao");
        List dmList = baseDao.getObjects(sql, viewStr, whereStr, orderStr,
                                         1, 10000);

        return new ViewForward(forward(mapping), dmList, myForm.getViews(), null, null);
    }
}
